package org.example.memora.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.RequiredArgsConstructor;
import org.example.memora.DTO.CommunityDTO;
import org.example.memora.DTO.CommunityUserDTO;
import org.example.memora.common.result.Result;
import org.example.memora.common.result.PageResult;
import org.example.memora.common.utils.UserHolder;
import org.example.memora.entity.Community;
import org.example.memora.entity.CommunityUser;
import org.example.memora.service.CommunityService;
import org.example.memora.service.CommunityUserService;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;

@RestController
@RequestMapping("/api/community")
@RequiredArgsConstructor
@Tag(name = "社群管理", description = "提供社群创建、查询、加入等相关操作接口")
public class CommunityController {

    private final CommunityService communityService;
    private final CommunityUserService communityUserService;

    @Operation(summary = "创建社群", description = "创建一个新的社群并保存到数据库中")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "社群创建成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/create")
    public Result<?> createCommunity(
            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "社群对象", required = true)
            @RequestBody CommunityDTO communitydto) {
        Community community=new Community();
        BeanUtil.copyProperties(communitydto,community);
        community.setCreatedAt(Instant.now());
        community.setUpdatedAt(Instant.now());
        community.setOwnerId(UserHolder.getUser().getUserId());
        community.setStatus(1);
        return communityService.createCommunity(community);
    }

    @Operation(summary = "获取加入的社群列表", description = "分页获取所有社群的信息")
    @Parameters({
            @Parameter(name = "page", description = "页码，从 1 开始", in = ParameterIn.QUERY),
            @Parameter(name = "size", description = "每页大小", in = ParameterIn.QUERY)
    })
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/list")
    public PageResult listCommunities(
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer size) {
        return communityService.listCommunities(page, size);
    }
    @Operation(summary = "获取所有的社群列表", description = "分页获取所有社群的信息，用于加群选择")
    @Parameters({
            @Parameter(name = "page", description = "页码，从 1 开始", in = ParameterIn.QUERY),
            @Parameter(name = "size", description = "每页大小", in = ParameterIn.QUERY)
    })
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/listAll")
    public PageResult listAllCommunities(
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer size) {
        return communityService.listAllCommunities(page, size);
    }

    @Operation(summary = "获取社群详情", description = "根据社群ID获取对应的详细信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "未找到对应社群"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/{id}")
    public Result<?> getCommunity(
            @Parameter(name = "id", description = "社群ID", in = ParameterIn.PATH, required = true)
            @PathVariable Long id) {
        return communityService.getCommunityById(id);
    }

//    @Operation(summary = "加入社群", description = "用户请求加入指定的社群")
//    @ApiResponses(value = {
//            @ApiResponse(responseCode = "200", description = "加入成功"),
//            @ApiResponse(responseCode = "400", description = "请求参数错误或已加入"),
//            @ApiResponse(responseCode = "401", description = "用户未登录"),
//            @ApiResponse(responseCode = "500", description = "服务器内部错误")
//    })
//    @PostMapping("/join")
//    public Result<?> joinCommunity(
//            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "加入社群信息", required = true)
//            @RequestBody CommunityUserDTO joinInfo) {
//        CommunityUser communityUser =new CommunityUser();
//        communityUser.setCommunityId(joinInfo.getCommunityId());
//        communityUser.setUserId(UserHolder.getUser().getUserId());
//        communityUser.setJoinedAt(Instant.now());
//        communityUser.setRole("member");
//        return communityUserService.joinCommunity(communityUser);
//    }

    @Operation(summary = "获取社群成员列表", description = "根据社群ID获取该社群的所有成员信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "社群不存在"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/members/{id}")
    public Result<?> getCommunityMembers(
            @Parameter(name = "id", description = "社群ID", in = ParameterIn.PATH, required = true)
            @PathVariable Long id) {
        return communityService.getMembersByCommunityId(id);
    }
}
